//
//  ViewConvertible.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import UIKit

public protocol ViewConvertible {
    func asView() throws -> UIView
    func asViewController() throws -> UIViewController
}

extension ViewConvertible {
    public var view: UIView? { try? asView() }
    public var asViewController: UIViewController? { try? asViewController() }
}

extension UIViewController: ViewConvertible {
    public func asView() throws -> UIView { return view }
    public func asViewController() throws -> UIViewController { return self }
}

extension UIView: ViewConvertible {
    public func asView() throws -> UIView { return self }
    public func asViewController() throws -> UIViewController {
        if let vc = UIApplication.lxs.topViewController {
            return vc
        }
        throw NSError(domain: "NotFoundViewController", code: -1, userInfo: nil)
    }
}
